package com.lysj.bhp.manager.controller;


import com.lysj.bhp.manager.base.ManagerContent;
import com.lysj.bhp.manager.domain.Privilege;
import com.lysj.bhp.manager.domain.Role;
import com.lysj.bhp.manager.domain.Staff;
import com.lysj.bhp.util.db.LongSetAndStringConvertUtil;
import com.lysj.bhp.util.json.GsonUtil;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.util.web.SelectItem;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("admin/staff_manager")
public class StaffManagerController extends ManagerContent {

    @GetMapping("list")
    public Resp list(Staff model) {
        model.setStatus(1);
        Specification specification = ParamUtil.createSpecification(model);
        List<Staff> staffList = staffRepository.findAll(specification, new Sort(Sort.Direction.ASC, "staffId"));
        return new Resp(staffList);
    }

    @PostMapping("add")
    public Resp add(Staff model) {
        if (ParamUtil.isBlack(model.getUsername())) {
            return new Resp(Resp.PARAM_ERROR, "用户名不能为空");
        }
        Staff staff = staffRepository.findByUsername(model.getUsername());
        if (staff != null) {
            return new Resp(Resp.PARAM_ERROR, "用户名已存在");
        }
        if (ParamUtil.isBlack(model.getPhone())) {
            return new Resp(Resp.PARAM_ERROR, "请填写用户电话号码");
        }
        if (ParamUtil.isBlack(model.getRealname())) {
            return new Resp(Resp.PARAM_ERROR, "请填写用户真实姓名");
        }

        //后期需要开放
        if (!ParamUtil.isBlack(model.getRoleId())) {
            Role role = roleRepository.findOne(model.getRoleId());
            if (null== role) {
                return new Resp(Resp.PARAM_ERROR, "查无该角色，请联系管理员");
            } else {
                model.setRoleName(role.getName());
                model.setPrivIds(role.getPrivIds());
                model.setPrivJson(role.getPrivJson());
            }
        } else {
            model.setRoleName("");
            model.setPrivIds("");
            model.setPrivJson("");
        }
        model.setCreateDate(new Date());
        model.setExpires(0L);
        model.setToken(ParamUtil.uuid());
        model.setPassword(ParamUtil.md5("123456"));
        model.setStatus(1); //设定用户状态为1,正常
        staffRepository.save(model);
        return new Resp("添加成功!密码为:123456");
    }

    @PostMapping("edit")
    public Resp edit(Staff model) {
        if (ParamUtil.isBlack(model.getUsername())) {
            return new Resp(Resp.PARAM_ERROR, "用户名不能为空");
        }
        Staff staff = staffRepository.findByUsername(model.getUsername());
        if (staff != null && staff.getStaffId() != model.getStaffId()) {
            return new Resp(Resp.PARAM_ERROR, "用户名已存在");
        }
        staff = staffRepository.findOne(model.getStaffId());
        if (ParamUtil.isBlack(model.getPhone())) {
            return new Resp(Resp.PARAM_ERROR, "请填写用户电话号码");
        }
        if (ParamUtil.isBlack(model.getRealname())) {
            return new Resp(Resp.PARAM_ERROR, "请填写用户真实姓名");
        }

        if (ParamUtil.isBlack(model.getRoleId())) {
            return new Resp(Resp.PARAM_ERROR, "角色不能为空");
        }
        Role role = roleRepository.findOne(model.getRoleId());
        if (role == null) {
            return new Resp(Resp.PARAM_ERROR, "查无该角色，请联系管理员");
        }
        staff.setUsername(model.getUsername());
        staff.setPhone(model.getPhone());
        staff.setRoleId(model.getRoleId());
        staff.setRealname(model.getRealname());
        staff.setRoleName(role.getName());
        staff.setPrivIds(role.getPrivIds());
        staff.setPrivJson(role.getPrivJson());
        staffRepository.save(staff);
        return new Resp("修改成功");

    }

    @PostMapping("remove")
    public Resp remove(Staff model) {
        if (ParamUtil.isBlack(model.getStaffId())) {
            return new Resp(Resp.PARAM_ERROR, "请选择用户");
        }
        if (model.isAdmin()) {
            return new Resp(Resp.PARAM_ERROR, "超级管理员,不允许删除!");
        }
        Staff staff = staffRepository.findOne(model.getStaffId());
        if (staff == null) {
            return new Resp(Resp.PARAM_ERROR, "该员工不存在，请联系管理员");
        }
        staff.setStatus(2);
        staffRepository.save(staff);
        return new Resp("删除成功");
    }


    @GetMapping("select_item")
    public Resp selectItem() {
        List<SelectItem> selectItems = staffRepository.selectItems();
        return new Resp(selectItems);
    }

    @PostMapping("/edit_priv")
    public Resp editPriv(Long staffId, String privIds) {
        if (ParamUtil.isBlack(staffId)) {
            return new Resp(Resp.PARAM_ERROR, "请选择员工!");
        }
        Staff staff = staffRepository.findOne(staffId);
        if (staff == null) {
            return new Resp(Resp.PARAM_ERROR, "请选择员工!");
        }
        Set<Long> privIdSet = new HashSet<>();
        if (!ParamUtil.isBlack(privIds)) {
            privIdSet = LongSetAndStringConvertUtil.string2LongSet(privIds);
        }
        staff.setPrivIds(LongSetAndStringConvertUtil.longSet2String(privIdSet));
        List<Privilege> privilegeList = privilegeRepository.findAll(privIdSet);
        staff.setPrivJson(GsonUtil.toJson(privilegeList));
        staffRepository.save(staff);
        return new Resp("修改用户权限成功!");
    }

    @PostMapping("reset_password")
    public Resp resetPassword(Staff model) {
        if (ParamUtil.isBlack(model.getStaffId())) {
            return new Resp(Resp.PARAM_ERROR, "请选择用户");
        }
        Staff staff = staffRepository.findOne(model.getStaffId());
        if (staff == null) {
            return new Resp(Resp.PARAM_ERROR, "该用户不存在，请联系管理员!");
        }
        staff.setPassword(ParamUtil.md5("123456"));
        staffRepository.save(staff);
        return new Resp("重置密码成功!密码为:123456");
    }


    @PostMapping("/change_password")
    public Resp changePassword(String oldPassword, String newPassword, String newPasswordTwo) {
        if (ParamUtil.isBlack(currentStaff().getStaffId())) {
            return new Resp(Resp.PARAM_ERROR, "请先登录");
        }
        Staff staff = staffRepository.findOne(currentStaff().getStaffId());
        if (staff == null) {
            return new Resp(Resp.PARAM_ERROR, "该用户不存在，请联系管理员!");
        }
        if (ParamUtil.isBlack(oldPassword)) {
            return new Resp(Resp.PARAM_ERROR, "请输入旧密码");
        }
        if (ParamUtil.isBlack(newPassword)) {
            return new Resp(Resp.PARAM_ERROR, "请输入新密码");
        }
        if (ParamUtil.isBlack(newPasswordTwo)) {
            return new Resp(Resp.PARAM_ERROR, "请再次输入新密码");
        }
        if (!newPassword.equals(newPasswordTwo)) {
            return new Resp(Resp.PARAM_ERROR, "两次输入的密码不相同");
        }
        if (!ParamUtil.md5(oldPassword).equals(currentStaff().getPassword())) {
            return new Resp(Resp.PARAM_ERROR, "旧密码不正确");
        }

        staff.setPassword(ParamUtil.md5(newPassword));
        staff.setExpires(System.currentTimeMillis());
        staffRepository.save(staff);
        return new Resp("修改密码成功");
    }

}
